package com.lsh.controller;

import com.lsh.constants.RedisMessageConstant;
import com.lsh.entity.Result;
import com.lsh.pojo.Order;
import com.lsh.service.OrderService;
import com.lsh.utils.RedisStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RedisStringUtils redisStringUtils;
    @Autowired
    private OrderService orderService;

    /**
     * 提交预约
     * @param paraMap
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String,String> paraMap){
        //先校验验证码
        //redis中的验证码
        String redisCode = redisStringUtils.get(RedisMessageConstant.SEND_KEYSTATUS + paraMap.get("telephone"));
        if (!StringUtils.isEmpty(redisCode)) {
            //不为空，校验验证码是否正确
            if (redisCode.equals(paraMap.get("validateCode"))) {
                //验证码相等
                //先把redis验证码删除 防止重复提交
                redisStringUtils.delete(RedisMessageConstant.SEND_SENDSTATUS+paraMap.get("telephone"));
                //设置预约类型
                paraMap.put("orderType",Order.ORDERTYPE_WEIXIN);
                //把数据添加到数据库
                Order order = orderService.submitOrder(paraMap);
                return new Result(true,"预约成功",order);
            }
            //验证码不相等
            return new Result(false,"验证码不正确，请重新发送验证码");
        }
        //redis中的验证码为空，代表着验证码过期
        return new Result(false,"请重新发送验证码");
    }

    @PostMapping("/findById")
    public Result findById(@RequestParam("id") Integer id){
        Map<String,Object> map = orderService.findById(id);
        return new Result(true,"订单详情查询成功",map);
    }
}
